package academic.concordia.bankofquebec.managedbean;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import academic.concordia.bankofquebec.bean.bankAccount.IBankAccountServices;
import academic.concordia.bankofquebec.bean.client.IClientBean;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.bean.virtualwallet.IVirtualWalletBean;
import academic.concordia.bankofquebec.bean.virtualwallet.VirtualWalletBean;
import academic.concordia.bankofquebec.entity.AccountAttributes;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.Transaction;
import academic.concordia.bankofquebec.entity.TransactionStatus;
import academic.concordia.bankofquebec.entity.VirtualWallet;

@ManagedBean(name="eWalletDepositManagedBean")
@SessionScoped
public class DepositToVirtualWalletManagedBean {
	
	private VirtualWallet virtualWallet;
	
	private double amount;
	
	@ManagedProperty(value="#{SessionData}")
	private SessionData sessionData;
	
	@ManagedProperty(value="#{ClientBean}")
	private IClientBean clientBean;
	
	@ManagedProperty(value="#{VirtualWalletBean}")
	private IVirtualWalletBean virtualWalletBean;
	
	@PostConstruct
	void initialize(){
		virtualWallet = new VirtualWallet();
	}
	
	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	
	public String deposit(){
		BankAccount fromAccount = sessionData.getClient().getProduct(AccountAttributes.CHECKINGS_ACCOUNT.name());
		Transaction transaction = virtualWalletBean.depositToVirtualWallet(virtualWallet, fromAccount, amount);
		if (transaction.getStatus().equals(TransactionStatus.REJECTED)){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			FacesMessage facesMessage = new FacesMessage(transaction.getMessage());
			facesContext.addMessage(null, facesMessage);
		}
		return "";
	}

	public VirtualWallet getVirtualWallet() {
		return virtualWallet;
	}

	public void setVirtualWallet(VirtualWallet eWallet) {
		this.virtualWallet = eWallet;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}

	public IClientBean getClientBean() {
		return clientBean;
	}

	public void setClientBean(IClientBean clientBean) {
		this.clientBean = clientBean;
	}

	public IVirtualWalletBean getVirtualWalletBean() {
		return virtualWalletBean;
	}

	public void setVirtualWalletBean(IVirtualWalletBean virtualWalletBean) {
		this.virtualWalletBean = virtualWalletBean;
	}
	
	
}
